package com.tianqu.tms.www.car.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAjaxAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.dto.TmsFinanciaDto;
import com.tianqu.tms.entity.TmsFinancial;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class GetFinanciaNameListAction extends TmsAjaxAction {
	private Logger logger = Logger.getLogger(CarMemberNameListAjaxAction.class);

	@Override
	public void process(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response, UserContext uc) throws TmsException {

		logger.debug(this.getClass().getName() + "#execute.");

		String query = request.getParameter("query");

		TmsFinancial financial = TmsFinancial.getInstance();
		List<TmsFinanciaDto> list = financial.find(query, "1", 30);
		List<Map> r = new ArrayList<Map>();
		Map map = null;
		for (TmsFinanciaDto tmsFinanciaDto : list) {
			map = new HashMap();
			map.put("value", tmsFinanciaDto.getShortName() + "[" + tmsFinanciaDto.getRealNamePy() + "]");
			map.put("data", tmsFinanciaDto.getFinanciaId());
			r.add(map);
		}

		JSONArray jsonArray = JSONArray.fromObject(JSONArray.fromObject(r));
		JSONObject json = new JSONObject();
		json.put("query", query);
		json.put("suggestions", jsonArray);

		responseValues(response, json.toString(), TEXT);

		logger.debug(this.getClass().getName() + "#doAction was completed.");
	}
}
